package cn.yuanqiao.archive.management.service.impl;

import cn.yuanqiao.archive.management.domain.YqPgg;
import cn.yuanqiao.archive.management.domain.YqZpgz;
import cn.yuanqiao.archive.management.domain.YqZpjl;
import cn.yuanqiao.archive.management.domain.YqZpmx;
import cn.yuanqiao.archive.management.mapper.YqPggMapper;
import cn.yuanqiao.archive.management.mapper.YqZpgzMapper;
import cn.yuanqiao.archive.management.mapper.YqZpjlMapper;
import cn.yuanqiao.archive.management.service.IYqPggService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 盘规格Service业务层处理
 * 
 * @author sy
 * @date 2023-07-10
 */
@Service
public class YqPggServiceImpl implements IYqPggService
{
    @Autowired
    private YqPggMapper yqPggMapper;

    @Autowired
    private YqZpgzMapper yqZpgzMapper;

    @Autowired
    private YqZpjlMapper yqZpjlMapper;

    /**
     * 查询盘规格
     * 
     * @param pggId 盘规格主键
     * @return 盘规格
     */
    @Override
    public YqPgg selectYqPggByPggId(Long pggId)
    {
        YqPgg yqPgg = new YqPgg();
        yqPgg.setPggId(pggId);
        return yqPggMapper.selectYqPggByPggId(yqPgg);
    }

    /**
     * 查询盘规格列表
     * 
     * @param yqPgg 盘规格
     * @return 盘规格
     */
    @Override
    public List<YqPgg> selectYqPggList(YqPgg yqPgg)
    {
        return yqPggMapper.selectYqPggList(yqPgg);
    }

    /**
     * 新增盘规格
     * 
     * @param yqPgg 盘规格
     * @return 结果
     */
    @Override
    public String insertYqPgg(YqPgg yqPgg)
    {
        int num=0;
        StringBuilder successMsg = new StringBuilder();
        if (yqPggMapper.isOnly(yqPgg)!=0){
            successMsg.append("名称：”"+yqPgg.getPggName()+"”已存在！");
        }else {
            LoginUser user = SecurityUtils.getLoginUser();

            if(user == null){
                user = new LoginUser();
                user.setUserId(1L);
            }

            yqPgg.setCreatorId(user.getUserId());
            yqPgg.setCreateTime(DateUtils.getNowDate());
            if(yqPggMapper.insertYqPgg(yqPgg)>0){
                successMsg.append("添加成功！");
            };
        }
//        return num;
        return successMsg.toString();
    }

    /**
     * 修改盘规格
     * 
     * @param yqPgg 盘规格
     * @return 结果
     */
    @Override
    public String updateYqPgg(YqPgg yqPgg)
    {
        StringBuilder successMsg = new StringBuilder();
        if (yqPggMapper.selectYqPggByPggId(yqPgg).getState()==1){
            successMsg.append("名称为：\""+yqPgg.getPggName()+"\"的盘规格已组盘，无法进行修改！\n");
        }else {
            if (yqPggMapper.isOnly(yqPgg)!=0){
                successMsg.append("名称：”"+yqPgg.getPggName()+"”已存在！");
            }else {
//                if (yqPgg.getState()==2){
//                    deleteYqZpjlByPggId(yqPgg.getPggId());
//                    successMsg.append("取消组盘成功！");
//                }
                LoginUser user = SecurityUtils.getLoginUser();

                if(user == null){
                    user = new LoginUser();
                    user.setUserId(1L);
                }

                yqPgg.setLastupdater(user.getUserId());
                yqPgg.setLastupdateTime(DateUtils.getNowDate());
                if(yqPggMapper.updateYqPgg(yqPgg)>0){
                    successMsg.append("修改成功！");
                };
            }
        }
        return successMsg.toString();
    }

    /**
     * 批量删除盘规格
     * 
     * @param pggIds 需要删除的盘规格主键
     * @return 结果
     */
    @Override
    public String deleteYqPggByPggIds(Long[] pggIds)
    {
        StringBuilder failMsg = new StringBuilder();
        int successNum = 0;
        int failNum = 0;
        YqPgg yqPgg = new YqPgg();
        for (int i = 0; i < pggIds.length; i++) {
            yqPgg = selectYqPggByPggId(pggIds[i]);
            if (yqPgg.getState()==1){
                failMsg.append("名称为：\""+yqPgg.getPggName()+"\"的盘规格已组盘，无法进行删除！\n");
                failNum++;
            }else {
                //同时删除组盘规则、组盘记录、组盘明细
                String userCode = SecurityUtils.getTenantCode();
                YqZpgz yqZpgz = new YqZpgz();
                yqZpgz.setPggId(yqPgg.getPggId());
                yqZpgz = yqZpgzMapper.selectYqZpgzByPggId(yqZpgz);
                if (null!=yqZpgz){
                    yqPggMapper.deleteYqPgg(yqPgg.getPggId(),yqZpgz.getZpgzId(),yqPgg.getTableName(),userCode+"_YQZPGZ",userCode+"_YQZPJL",userCode+"_YQZPMX");
                }else {
                    yqPggMapper.deleteYqPggByPggId(yqPgg);
                }
                successNum++;
            }
        }
        if (failNum==0){
            return "删除成功"+successNum+"条！";
        }else {
            return "删除成功"+successNum+"条，删除失败"+failNum+"条。\n失败原因："+failMsg;
        }
    }

    /**
     * 删除盘规格信息
     * 
     * @param pggId 盘规格主键
     * @return 结果
     */
    @Override
    public int deleteYqPggByPggId(Long pggId)
    {
        YqPgg yqPgg = new YqPgg();
        yqPgg.setPggId(pggId);
        return yqPggMapper.deleteYqPggByPggId(yqPgg);
    }

    /**
     * 根据盘规格ID删除组盘记录
     *
     * @param pggId 盘规格主键
     * @return 结果
     */
    @Override
    public String deleteYqZpjlByPggId(Long pggId){
        //不删除记录，将记录的是否最新改为0
        StringBuilder successMsg = new StringBuilder();
        String userCode = SecurityUtils.getTenantCode();
        String tableName = "";
        if(StringUtils.isEmpty(userCode)){
            tableName = "YQZPJL";
        }else {
//                tableName = userCode.toUpperCase()+"_YQZPJL";
            tableName = "YQZPJL";
        }
        if(yqZpjlMapper.deleteYqZpjlByPggId(tableName,pggId)>0){
            successMsg.append("删除成功！");
        }
        return successMsg.toString();
    }


    /**
     * 根据盘规格ID查询组盘记录
     *
     * @param pggId 盘规格主键
     * @return 结果
     */
    @Override
    public List<Map<YqZpjl,List<YqZpmx>>> selectYqZpjlListByPgg(Long pggId){
        List<Map<YqZpjl,List<YqZpmx>>> plan = new LinkedList<>();
        String userCode = SecurityUtils.getTenantCode();
        String yqzpjlTable = "";
        String yqzpmxTable = "";
        if(StringUtils.isEmpty(userCode)){
            yqzpjlTable = "YQZPJL";
            yqzpmxTable = "YQZPMX";
        }else {
            yqzpjlTable = userCode.toUpperCase()+"_YQZPJL";
            yqzpmxTable = userCode.toUpperCase()+"_YQZPMX";
//            yqzpjlTable = "YQZPJL";
//            yqzpmxTable = "YQZPMX";
        }
        List<YqZpjl> yqZpjlList =  yqZpjlMapper.selectYqZpjlListByPgg(yqzpjlTable,pggId);
        for (YqZpjl yqZpjl : yqZpjlList) {
            yqZpjl.setZlConvert(YqZpgzServiceImpl.convertToReadableSize(Double.valueOf(yqZpjl.getZl())));
            List<YqZpmx> yqZpmxList =  yqZpjlMapper.selectYqZpmxList(yqzpmxTable,yqZpjl.getZpjlId());
            Iterator<YqZpmx> iterator = yqZpmxList.iterator();

            // 使用迭代器遍历List
            while (iterator.hasNext()) {
                YqZpmx yqZpmx = iterator.next();
                yqZpmx.setDocFileSizeConvert(YqZpgzServiceImpl.convertToReadableSize(Double.valueOf(yqZpmx.getDocFileSize())));
            }
            Map<YqZpjl,List<YqZpmx>> z = new HashMap<>();
            z.put(yqZpjl,yqZpmxList);
            plan.add(z);
        }
        return plan;
    }

    /**
     * 根据panguigeID解除组盘状态
     *
     * @param pggId 盘规格主键
     * @return 结果
     */
    @Override
    public String cancel(Long pggId) {
        //在盘规格表中将state=0,在规则表中将is_use=0,在记录表中is_last=0,档案is_zp=0
        YqZpgz yqZpgz = new YqZpgz();
        yqZpgz.setPggId(pggId);
        int successNum = 0;
        String sussessMsg = "";
        if (null!=yqZpgzMapper.selectYqZpgzByPggId(yqZpgz)){
            YqPgg yqPgg = new YqPgg();
            YqZpjl yqZpjl = new YqZpjl();
            YqZpmx yqZpmx = new YqZpmx();
            yqZpgz = yqZpgzMapper.selectYqZpgzByPggId(yqZpgz);
            String userCode = SecurityUtils.getTenantCode();
            String categoryTable = userCode + "_CATEGORY";
            String DA_TABLE = "";
            List<Map<String,String>> categoryMap=yqZpgzMapper.selectCategory(categoryTable,yqZpgz.getCategoryId());
            //根据整理方式选择分表
            if (!String.valueOf(categoryMap.get(0).get("TYPE")).equals("0")){
                DA_TABLE = categoryMap.get(0).get("FOLDER_TABLE_NAME");
            }else {
                DA_TABLE = categoryMap.get(0).get("FILE_TABLE_NAME");
            }
            successNum = yqZpjlMapper.updateYqZpjlCancel(yqPgg.getTableName(),yqZpgz.getTableName(),yqZpjl.getTableName(),yqZpmx.getTableName(),DA_TABLE,pggId,yqZpgz.getZpgzId());
            if (successNum == 0){
                sussessMsg = "取消组盘失败！";
            }else {
                sussessMsg = "取消组盘失败！";
            }
        }
        return sussessMsg;
    }
}
